﻿using System;
using System.Net;
using NUnit.Framework;
using PS.WssSync.Tests.WebsService;
using PS.Utils.FPRPC;

namespace PS.WssSync.Tests
{
    [TestFixture]
    public class Urls
    {
        public string TestFunc(Func<string ,string, string> action)
        {
            return action("Hello", "World!");
        }

        public string FuncToTest(string s, string w)
        {
            return s + w;
        }

        [Test]
        public void RunTestFunc()
        {
            TestFunc(FuncToTest);

            bool condition = false;

            Func<string, string, string> a1;
            if (condition)
                a1 = (s, w) => s + w;
            else
                a1 = FuncToTest;

            TestFunc(a1);

            Func<int, int> factorial = null;
            factorial = i =>
                            {
                                if (i > 2)
                                {
                                    return i * factorial(i - 1);
                                }
                                else
                                {
                                    return 1;
                                }
                            };
            Func<int, Func<int>> createFactorialFunc =
                i =>
                    {
                        Func<int> result = () => 1;

                        for (int j = 2; j <= i; j++)
                        {
                            int xx = result();
                            result = () => xx*j;
                        }

                        return result;
                    };

            Func<int> myFac = createFactorialFunc(5);

            Assert.IsTrue(myFac() == 60);

            Assert.IsTrue(factorial(5) == 60);

            TestFunc(delegate (string s, string w)
                         {
                             return s + w;
                         });

            TestFunc((s, w) => s + w);

            TestFunc((s, w) =>
                         {
                             return s + w;
                         });

            Action<string> myAction 
                = s => Console.WriteLine(s);

            myAction = delegate(string s) { Console.WriteLine(s); };

            myAction("dsadsa");
        }

        [Test]
        public void GetWebRelativeUrl()
        {
            Webs webService = new Webs();
            webService.Credentials = CredentialCache.DefaultCredentials;
            string pageUrl = "http://Saber-test:81/test/asdasd/dsadsad/";

            string result = webService.WebUrlFromPageUrl(pageUrl);

            Assert.IsNotEmpty(result);
            Assert.IsTrue(result.Equals("http://Saber-test:81/test", StringComparison.InvariantCultureIgnoreCase));
        }

        [Test]
        public void GetWebRelativeUrl2()
        {
            FrontPageRPC rpc = new FrontPageRPC();
            string pageUrl = "http://Saber-test:81/test/asdasd/dsadsad/";

            WebUrl webUrl = rpc.UrlToWebUrl(pageUrl);

            Assert.IsNotEmpty(webUrl.SiteUrl);
            Assert.IsTrue(webUrl.SiteUrl.Equals("http://Saber-test:81/test", StringComparison.InvariantCultureIgnoreCase));
        }

    }
}